package org.duiduo.utils;

import cn.hutool.core.util.StrUtil;

import java.util.Scanner;

/**
 * 输入工具类
 *
 * @author liangze
 * @date 2023/6/27 15:39
 */
public class ScannerUtils {

    /**
     * 读取控制台内容
     *
     * @return 内容
     */
    public static String scannerString(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + ": ");
        System.out.println(help);
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StrUtil.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new IllegalArgumentException("请输入正确的" + tip + "!");
    }

    /**
     * 读取控制台内容
     *
     * @return 内容
     */
    public static int scannerInt(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + ": ");
        System.out.println(help);
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StrUtil.isNotEmpty(ipt)) {
                try {
                    return Integer.parseInt(ipt);
                } catch (Exception e) {
                    throw new IllegalArgumentException("请输入正确的" + tip + "!");
                }
            }
        }
        throw new IllegalArgumentException("请输入正确的" + tip + "!");
    }

}
